# -*- coding: utf-8 -*-

# @Time : 2020/7/4 18:00 
# @Author : dzzhyk
# @File : csv_transform.py 
# @Software: PyCharm

# 简单清洗csv格式


import csv
import re


def transform(filename, save_to):
    with open(filename, "r", encoding="utf-8") as fin:
        reader = csv.reader(fin)
        result = list(reader)

        # 2年车龄／ 2.2万公里／ 东莞

        fout = open(save_to, "w", encoding="utf-8")
        for line in result:
            temp = line[0]
            temp += "," + line[1]
            temp += "," + line[2]
            strings = str(line[3]).split('／')
            temp += "," + re.findall(r"\d", strings[0])[0]
            temp += "," + re.findall(r"[-+]?\d*.\d+|\d+", strings[1].strip())[0]
            temp += "," + strings[2].strip()
            temp += "," + line[4]
            temp += "," + line[5]
            temp += "\n"
            fout.write(temp)
            fout.flush()
        fout.close()
        fin.close()


if __name__ == '__main__':
    transform("../temp/tiantian.csv", "../data/tiantian/cleaned/tiantian_cleaned.csv")